package com.sms.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sms.db.DBOperator;

public class DeliverReportServlet extends HttpServlet {

	private static Connection conn = null;
	/**
	 * Constructor of the object.
	 */
	public DeliverReportServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String sql = null;
		PreparedStatement pstmt = null;
		String retMsg = null;
		ResultSet rs = null;
		try {
			if(conn == null || conn.isClosed())
			{
				conn = DBOperator.getConnection();
				System.out.println("=============getConnection=============");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//获取返回参数信息
		try{
			String msgid = request.getParameter("msgid");
			String stat = request.getParameter("stat");
			String phone = request.getParameter("phone");
			int sendmsgId = 0;
			int waitsendId = 0;
			int feetype = 0;  //默认为不按状态报告收费
			int parts = 0;
			int task_id = 0;
			if(msgid != null && stat != null && phone != null)
			{
				retMsg = "success";  //获取参数正确
				
				sql = "select id,task_id,serialno,feetype,parts from t_sendmsg where status='1' and retid='"+msgid+"' and phone='"+phone+"' limit 1";
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				if(rs.next()){
					sendmsgId = rs.getInt("id");
					task_id = rs.getInt("task_id");
					waitsendId =  rs.getInt("serialno");
					feetype = rs.getInt("feetype");
					parts = rs.getInt("parts");
				}
				rs.close();
				pstmt.close();
				
				if("0".equals(stat)){  //短信发送成功
					sql = "update t_sendmsg set status='2',retmsg='状态报告返回成功' where id=" + sendmsgId;
					pstmt = conn.prepareStatement(sql);
					pstmt.executeUpdate();
					pstmt.close();
					
					sql = "update t_waitsend set status='2' where id=" + waitsendId;
					pstmt = conn.prepareStatement(sql);
					pstmt.executeUpdate();
					pstmt.close();
					
					//本身为已发送短信时，短信数已经记录为发送成功
//					if(task_id != 0){ //如果不是单笔短信
//						sql = "update t_task set succ_totals=succ_totals+"+parts+",fail_totals=fail_totals-"+parts+" where id="+task_id;
//						pstmt = conn.prepareStatement(sql);
//						pstmt.executeUpdate();
//						pstmt.close();
//					}
				}else{
					sql = "update t_sendmsg set status='3',retmsg='状态报告返回失败' where id=" + sendmsgId;
					pstmt = conn.prepareStatement(sql);
					pstmt.executeUpdate();
					pstmt.close();
					
					sql = "update t_waitsend set status='3' where id=" + waitsendId;
					pstmt = conn.prepareStatement(sql);
					pstmt.executeUpdate();
					pstmt.close();
				
					if(1 == feetype){//按状态报告收费
						sql = "update user_info_sms set smscount=smscount+" + parts + " where user_id=(select user_id from t_sendmsg where id=" + sendmsgId+" limit 1)";
						pstmt = conn.prepareStatement(sql);
						pstmt.executeUpdate();
						pstmt.close();
						//按状态报告计费，需同时更新虚拟成功数
						if(task_id != 0){ //如果不是单笔短信
							sql = "update t_task set succ_totals=succ_totals-"+parts+",fail_totals=fail_totals+"+parts+",succ_totals2=succ_totals2-"+parts+" where id="+task_id;
							pstmt = conn.prepareStatement(sql);
							pstmt.executeUpdate();
							pstmt.close();
						}

					}else{
						//不按状态报告计费，不需要更新虚拟成功数
						if(task_id != 0){ //如果不是单笔短信
							sql = "update t_task set succ_totals=succ_totals-"+parts+",fail_totals=fail_totals+"+parts+" where id="+task_id;
							pstmt = conn.prepareStatement(sql);
							pstmt.executeUpdate();
							pstmt.close();
						}
					}
				}
			}
			else
				retMsg = "fail";
		}catch(Exception e){
			e.printStackTrace();
		}
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println(retMsg);
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
